function divideOridinaryText(text) {
    let dividedText = [];

    for (const i of text) {
        dividedText.push({ text: i, type: "" });
    }

    return dividedText;
}

// ordinary text is divided into single chars.
function divideSentence(sentence, splitterData, splitterIndex) {
    if (sentence === "") {
        return [];
    }

    if (splitterIndex === splitterData.length) {
        return divideOridinaryText(sentence);
    }

    const splittedParts = sentence.split(splitterData[splitterIndex].entity);

    let currentResult = [];

    // insert current splitter after each splitted part except the last part
    // if the sentence doesn't contain current splitter, this also works because
    // splittedParts.length will be 1 and this loop won't be executed at all.

    // push is the most efficient way to concat arrays according to our benchmark.
    for (let i = 0; i < splittedParts.length - 1; i++) {
        currentResult.push(
            ...divideSentence(splittedParts[i], splitterData, splitterIndex + 1),
            {
                text: splitterData[splitterIndex].entity,
                type: splitterData[splitterIndex].type
            }
        );
    }

    currentResult.push(
        ...divideSentence(splittedParts[splittedParts.length - 1], splitterData, splitterIndex + 1)
    );

    return currentResult;
}

function generateRandomColor() {
    const rgbMin = 50;
    const rgbMax = 200;
    const r = Math.round(rgbMin + Math.random() * (rgbMax - rgbMin));
    const g = Math.round(rgbMin + Math.random() * (rgbMax - rgbMin));
    const b = Math.round(rgbMin + Math.random() * (rgbMax - rgbMin));

    return `rgb(${r},${g},${b})`;
}

function generateSentencePartColors(splitterData) {
    let sentencePartColors = {};

    for (const i of splitterData) {
        if (i.type !== "" && !(i.type in sentencePartColors)) {
            sentencePartColors[i.type] = generateRandomColor();
       }
    }

    return sentencePartColors;
}

export { divideSentence, generateSentencePartColors };